package test.poi;

import java.io.File;

import com.aspose.words.Document;
import com.aspose.words.ImageSaveOptions;
import com.aspose.words.SaveFormat;

public class WordToImage {

    /**
     * 
     * Word转图片方法
     * 
     * @param wordFilePath Word文件路径
     * 
     * @param imagePath    图片保存路径
     * 
     * @param format       图片格式，可选值：jpg、png、bmp、gif、tiff
     * 
     */

    public static void wordToImage(String wordFilePath, String imagePath) {

        try {

            Document doc = new Document(wordFilePath);

            ImageSaveOptions options = new ImageSaveOptions(SaveFormat.JPEG);

            // 设置图片保存路径

            options.setPrettyFormat(true);

            options.setUseHighQualityRendering(true);

//            options.setImageResolution(300);

            // 导出图片

            for (int i = 0; i < doc.getPageCount(); i++) {

                doc.save(imagePath + "\\" + i + ".jpg", options);

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    public static void main(String[] args) {

        String wordFilePath = "D:\\poi\\pieChart.docx";

        String imagePath = "C:\\image";

        File file = new File(imagePath);

        if (!file.exists()) {

            file.mkdirs();

        }
        wordToImage(wordFilePath, imagePath);
    }

}